﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class ProvinceController : Controller
    {

        public IProvinceService ProvinceService { get; set; }


        [CheckPermission("Province.List")]
        // GET: Province
        public ActionResult List()
        {
            var provinces = ProvinceService.GetAll();
            return View(provinces);
        }


        [CheckPermission("Province.Add")]
        public ActionResult AddNew()
        {
            return View();
        }



        [CheckPermission("Province.Add")]
        [HttpPost]
        public ActionResult AddNew(string name)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }

            var province = ProvinceService.GetByName(name);

            if (province != null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = name + "已存在" });
            }

            long provinceId = ProvinceService.AddNew(name);

            return Json(new AjaxResult { Status = "ok" });
        }



        [CheckPermission("Province.Edit")]
        [HttpGet]
        public ActionResult Edit(long id)
        {
            var province = ProvinceService.GetById(id);

            if (province == null)
            {
                return Content("未找到ID为" + id + "的省份");
            }

            return View(province);
        }



        [CheckPermission("Province.Edit")]
        [HttpPost]
        public ActionResult Edit(long id, string name)
        {
            //验证数据
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }

            //获取数据判断省份是否存在
            var province = ProvinceService.GetById(id);

            if (province == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "未找到ID为" + id + "的省份,请检查是否已被删除！" });
            }

            //获取名称判断修改的省份是否已存在并且不为自身原有名称
            var dataProvince = ProvinceService.GetByName(name);

            if (dataProvince != null && province.Id != dataProvince.Id)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = name + "省份已存在" });
            }

            ProvinceService.Update(id, name);

            return Json(new AjaxResult { Status = "ok" });
        }



        [CheckPermission("Province.Delete")]
        [HttpPost]
        public ActionResult Delete(long provinceId)
        {
            ProvinceService.MarkDeleted(provinceId);

            return Json(new AjaxResult { Status = "ok" });
        }



        [CheckPermission("Province.Delete")]
        [HttpPost]
        public ActionResult BatchDel(long[] provinceIds)
        {
            foreach (var provId in provinceIds)
            {
                ProvinceService.MarkDeleted(provId);
            }

            return Json(new AjaxResult { Status = "ok" });
        }
    }
}